home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_xemacs.idb / usr / freeware / lib / xemacs-20.4 / lisp / packages / balloon-help.el.z / balloon-help.el
Encoding:
Text File  |  1998-05-21  |  19.0 KB  |  543 lines

  1. ;;; Balloon help for XEmacs (requires 19.15 or later)
  2. ;;; Copyright (C) 1995, 1997 Kyle E. Jones
  3. ;;;
  4. ;;; This program is free software; you can redistribute it and/or modify
  5. ;;; it under the terms of the GNU General Public License as published by
  6. ;;; the Free Software Foundation; either version 1, or (at your option)
  7. ;;; any later version.
  8. ;;;
  9. ;;; This program is distributed in the hope that it will be useful,
  10. ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. ;;; GNU General Public License for more details.
  13. ;;;
  14. ;;; A copy of the GNU General Public License can be obtained from this
  15. ;;; program's author (send electronic mail to kyle@uunet.uu.net) or from
  16. ;;; the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA
  17. ;;; 02139, USA.
  18. ;;;
  19. ;;; Send bug reports to kyle@wonderworks.com
  20.  
  21. ;; Balloon help pops up a small frame to display help text
  22. ;; relating to objects that the mouse cursor passes over.
  23. ;; 
  24. ;; Installation:
  25. ;;
  26. ;; Byte-compile the file balloon-help.el (with M-x byte-compile-file)
  27. ;; and put the .elc file in a directory in your load-path.  Add the
  28. ;; following line to your .emacs:
  29. ;;
  30. ;; (require 'balloon-help)
  31. ;; (balloon-help-mode 1)
  32. ;;
  33. ;; The balloon-help frame is a transient window that is not
  34. ;; normally decorated by window managers, so the following
  35. ;; window manager directives may not be needed.  But if they
  36. ;; are:
  37. ;;
  38. ;; For ol[v]wm use this in .Xdefaults:
  39. ;;    olvwm.NoDecor: balloon-help
  40. ;;      or
  41. ;;    olwm.MinimalDecor: balloon-help
  42. ;;
  43. ;; For fvvm version 1 use this in your .fvwmrc:
  44. ;;    NoTitle balloon-help
  45. ;; or
  46. ;;    Style "balloon-help" NoTitle, NoHandles, BorderWidth 0
  47. ;;
  48. ;; For twm use this in your .twmrc:
  49. ;;    NoTitle { "balloon-help" }
  50. ;;
  51.  
  52. (provide 'balloon-help)
  53.  
  54. (require 'custom)
  55.  
  56. (defgroup balloon-help nil
  57.   "Balloon-help support in XEmacs"
  58.   :group 'frames)
  59.  
  60. (defvar balloon-help-version "1.06"
  61.   "Version string for Balloon Help.")
  62.  
  63. (defcustom balloon-help-mode nil
  64.   "*Non-nil means Balloon help mode is enabled."
  65.   :type 'boolean
  66.   :set (lambda (symbol value)
  67.      (balloon-help-mode (or value 0)))
  68.   :initialize 'custom-initialize-default
  69.   :require 'balloon-help
  70.   :group 'balloon-help)
  71.  
  72. (defcustom balloon-help-timeout 1500
  73.   "*Display help after this many milliseconds of mouse inactivity."
  74.   :type 'integer
  75.   :group 'balloon-help)
  76.  
  77. (defcustom balloon-help-foreground "black"
  78.   "*The foreground color for displaying balloon help text."
  79.   :type 'string
  80.   :group 'balloon-help)
  81.  
  82. (defcustom balloon-help-background "gray80"
  83.   "*The background color for the balloon help frame."
  84.   :type 'string
  85.   :group 'balloon-help)
  86.  
  87. (defcustom balloon-help-background-pixmap ""
  88.   "*The background pixmap for the balloon help frame."
  89.   :type 'string
  90.   :group 'balloon-help)
  91.  
  92. (defcustom balloon-help-font "variable"
  93.   "*The font for displaying balloon help text."
  94.   :type 'string
  95.   :group 'balloon-help)
  96.  
  97. (defcustom balloon-help-border-color "black"
  98.   "*The color for displaying balloon help frame's border."
  99.   :type 'string
  100.   :group 'balloon-help)
  101.  
  102. (defcustom balloon-help-border-width 1
  103.   "*The width of the balloon help frame's border."
  104.   :type 'integer
  105.   :group 'balloon-help)
  106.  
  107. (defcustom balloon-help-use-sound nil
  108.   "*Non-nil value means play a sound to herald the appearance
  109. and disappearance of the help frame.
  110.  
  111. `balloon-help-appears' will be played when the frame appears.
  112. `balloon-help-disappears' will be played when the frame disappears.
  113.  
  114. See the documentation for the function load-sound-file to see how
  115. define sounds."
  116.   :type 'boolean
  117.   :group 'balloon-help)
  118.  
  119. (defcustom balloon-help-frame-name "balloon-help"
  120.   "*The frame name to use for the frame to display the balloon help."
  121.   :type 'string
  122.   :group 'balloon-help)
  123.  
  124. (defcustom balloon-help-aggressively-follow-mouse nil
  125.   "*Non-nil means the balloon should move with the mouse even if the mouse
  126. is over the same object as the last mouse motion event."
  127.   :type 'boolean
  128.   :group 'balloon-help)
  129.  
  130. ;;;
  131. ;;; End of user variables.
  132. ;;;
  133.  
  134. (defvar mouse-motion-hook mouse-motion-handler
  135.   "Hooks to be run whenever the user moves the mouse.
  136. Each hook is called with one argument, the mouse motion event.
  137. This hooks variable does not exist unless the \"balloon-help\" library
  138. has been loaded.")
  139.  
  140. (defun mouse-motion-hook (event)
  141.   "Run the hooks attached to mouse-motion-hook."
  142.   (run-hook-with-args 'mouse-motion-hook event))
  143.  
  144. (setq mouse-motion-handler 'mouse-motion-hook)
  145.  
  146. (defvar balloon-help-frame nil
  147.   "Balloon help is displayed in this frame.")
  148.  
  149. (defvar balloon-help-junk-frame nil
  150.   "Junk parent frame of balloon-help-frame.")
  151.  
  152. (defvar balloon-help-help-object nil
  153.   "Object that the mouse is over that has a help property, nil otherwise.")
  154.  
  155. (defvar balloon-help-help-object-x nil
  156.   "Last horizontal mouse position over balloon-help-help-object.")
  157.  
  158. (defvar balloon-help-help-object-y nil
  159.   "Last vertical mouse position over balloon-help-help-object.")
  160.  
  161. (defvar balloon-help-buffer (get-buffer-create " *balloon-help*")
  162.   "Buffer used to display balloon help.")
  163.  
  164. (defvar balloon-help-timeout-id nil
  165.   "Timeout id for the balloon help timeout.")
  166.  
  167. (defvar balloon-help-display-pending nil
  168.   "Non-nil value means the help frame will be visible as soon
  169. as the X server gets around to displaying it.  Nil means it
  170. will be invisible as soon as the X server decides to hide it.")
  171.  
  172. (defun balloon-help-mode (&optional arg)
  173.   "Toggle Balloon Help mode.
  174. With arg, turn Balloon Help mode on iff arg is positive.
  175.  
  176. With Balloon Help enabled, a small frame is displayed whenever
  177. the mouse rests on an object that has a help property of some
  178. kind.  The text of that help property is displayed in the frame.
  179.  
  180. For extents, the 'balloon-help' property is
  181. checked.
  182.  
  183. For toolbar buttons, the help-string slot of the toolbar button
  184. is checked.
  185.  
  186. If the value is a string, it is used as the help message.
  187.  
  188. If the property's value is a symbol, it is assumed to be the name
  189. of a function and it will be called with one argument, the object
  190. under the mouse, and the return value of that function will be
  191. used as the help message."
  192.   (interactive "P")
  193.   (setq balloon-help-mode (or (and arg (> (prefix-numeric-value arg) 0))
  194.                   (and (null arg) (null balloon-help-mode))))
  195.   (if (null balloon-help-mode)
  196.       (balloon-help-undisplay-help)))
  197.  
  198. (defun balloon-help-displayed ()
  199.   (and (frame-live-p balloon-help-frame)
  200.        (frame-visible-p balloon-help-frame)
  201.        (eq (frame-device balloon-help-frame) (selected-device))))
  202.  
  203. (defun balloon-help (&optional event)
  204.   "Display Balloon Help for the object under EVENT.
  205. If EVENT is nil, then point in the selected window is used instead.
  206. See the documentation for balloon-help-mode to find out what this means.
  207. This command must be bound to a mouse event."
  208.   (interactive "e")
  209.   (unless (device-on-window-system-p)
  210.     (error "Cannot display balloon help on %s device" (device-type)))
  211.   (let ((balloon-help-mode t))
  212.     (balloon-help-motion-hook event))
  213.   (when balloon-help-timeout-id
  214.     (disable-timeout balloon-help-timeout-id)
  215.     (setq balloon-help-timeout-id nil))
  216.   (balloon-help-display-help))
  217.  
  218. (defun balloon-help-motion-hook (event)
  219.   (cond
  220.    ((null balloon-help-mode) t)
  221.    (t
  222.     (let* ((buffer (if event (event-buffer event) (current-buffer)))
  223.        (frame (if event (event-frame event) (selected-frame)))
  224.        (point (if event (event-point event) (point)))
  225.        (modeline-point (if event (event-modeline-position event)))
  226.        (modeline-extent (and modeline-point
  227.                  (map-extents
  228.                   (function (lambda (e ignored) e))
  229.                   (symbol-value-in-buffer
  230.                    'generated-modeline-string
  231.                    buffer)
  232.                   modeline-point modeline-point
  233.                   nil nil
  234.                   'balloon-help)))
  235.        (glyph-extent (and event (event-glyph-extent event)))
  236.        (glyph-extent (if (and glyph-extent
  237.                   (extent-property glyph-extent
  238.                            'balloon-help))
  239.                  glyph-extent))
  240.        (extent (and point
  241.             (extent-at point buffer 'balloon-help)))
  242.        (button (and event (event-toolbar-button event)))
  243.        (button (if (and button (toolbar-button-help-string button))
  244.                button
  245.              nil))
  246.        (object (or modeline-extent glyph-extent extent button))
  247.        (id balloon-help-timeout-id))
  248.       (if (null object)
  249.       (if (and balloon-help-frame
  250.            (not (eq frame balloon-help-frame)))
  251.           (progn
  252.         (setq balloon-help-help-object nil)
  253.         (when id
  254.           (disable-timeout id)
  255.           (setq balloon-help-timeout-id nil))
  256.         (if (balloon-help-displayed)
  257.             (balloon-help-undisplay-help))))
  258.     (let* ((params (frame-parameters frame))
  259.            (top (cdr (assq 'top params)))
  260.            (left (cdr (assq 'left params)))
  261.            (xtop-toolbar-height
  262.         (if (and (specifier-instance top-toolbar-visible-p frame)
  263.              (specifier-instance top-toolbar frame))
  264.             (specifier-instance top-toolbar-height frame)
  265.           0))
  266.            (xleft-toolbar-width
  267.         (if (and (specifier-instance left-toolbar-visible-p frame)
  268.              (specifier-instance left-toolbar frame))
  269.             (specifier-instance left-toolbar-width frame)
  270.           0))
  271.            (menubar-height
  272.         (if (and buffer
  273.              (specifier-instance menubar-visible-p)
  274.              (save-excursion (set-buffer buffer) current-menubar))
  275.             22 0)))
  276.       (setq balloon-help-help-object-x
  277.         (if event
  278.             (+ left xleft-toolbar-width
  279.                (event-x-pixel event))
  280.           (/ (* (device-pixel-width) 2) 5))
  281.         balloon-help-help-object-y
  282.         (if event
  283.             (+ top xtop-toolbar-height menubar-height
  284.                (event-y-pixel event))
  285.           (/ (* (device-pixel-height) 2) 5))))
  286.     (cond ((eq frame balloon-help-frame) t)
  287.           ((eq object balloon-help-help-object)
  288.            (if (and (balloon-help-displayed)
  289.             balloon-help-aggressively-follow-mouse)
  290.            (balloon-help-move-help-frame)))
  291.           ((balloon-help-displayed)
  292.            (setq balloon-help-help-object object)
  293.            (balloon-help-display-help))
  294.           (t
  295.            (setq balloon-help-help-object object)
  296.            (if id
  297.            (disable-timeout id))
  298.            (setq balloon-help-timeout-id
  299.              (add-timeout (/ balloon-help-timeout 1000.0)
  300.                   (function balloon-help-display-help)
  301.                   nil)))))))))
  302.  
  303. (defun balloon-help-display-help (&rest ignored)
  304.   (setq balloon-help-timeout-id nil)
  305.   (if (and balloon-help-help-object (device-on-window-system-p))
  306.       (let* ((object balloon-help-help-object)
  307.          (help (or (and (extent-live-p object)
  308.                 (extent-property object 'balloon-help))
  309.                (and (toolbar-button-p object)
  310.                 (toolbar-button-help-string object))
  311.                (and (stringp object) object))))
  312.     ;; if help is non-null and is not a string, run it as
  313.     ;; function to produuce the help string.
  314.     (if (or (null help) (not (symbolp help)))
  315.         nil
  316.       (condition-case data
  317.           (setq help (funcall help object))
  318.         (error
  319.          (setq help (format "help function signaled: %S" data)))))
  320.     (if (stringp help)
  321.         (save-excursion
  322.           (if (or (not (frame-live-p balloon-help-frame))
  323.               (not (eq (selected-device)
  324.                    (frame-device balloon-help-frame))))
  325.           (setq balloon-help-frame (balloon-help-make-help-frame)))
  326.           (set-buffer balloon-help-buffer)
  327.           (erase-buffer)
  328.           (insert help)
  329.           (if (not (bolp))
  330.           (insert ?\n))
  331. ;;;          ;; help strings longer than 2 lines have the last
  332. ;;;          ;; line stolen by the minibuffer, so make sure the
  333. ;;;          ;; last line is blank.  Make the top line blank for
  334. ;;;          ;; some symmetry.
  335. ;;;          (if (< 2 (count-lines (point-min) (point-max)))
  336. ;;;          (progn
  337. ;;;            (insert ?\n)
  338. ;;;            ;; add a second blank line at the end to
  339. ;;;            ;; prevent the modeline bar from clipping the
  340. ;;;            ;; descenders of the last line of text.
  341. ;;;            (insert ?\n)
  342. ;;;            (goto-char (point-min))
  343. ;;;            (insert ?\n)))
  344.           ;; indent everything by a space for readability
  345.            (indent-rigidly (point-min) (point-max) 1)
  346.           (balloon-help-set-frame-properties)
  347.           (balloon-help-resize-help-frame)
  348.           (balloon-help-move-help-frame)
  349.           (balloon-help-expose-help-frame))))))
  350.  
  351. (defun balloon-help-undisplay-help ()
  352.   (balloon-help-hide-help-frame))
  353.  
  354. (defun balloon-help-hide-help-frame ()
  355.   (if (balloon-help-displayed)
  356.       (progn
  357.     (make-frame-invisible balloon-help-frame)
  358.     (if (and balloon-help-use-sound balloon-help-display-pending)
  359.         (play-sound 'balloon-help-disappears))
  360.     (setq balloon-help-display-pending nil))))
  361.  
  362. (defun balloon-help-expose-help-frame ()
  363.   (if (not (balloon-help-displayed))
  364.       (progn
  365.     (make-frame-visible balloon-help-frame)
  366.     (if (and balloon-help-use-sound (null balloon-help-display-pending))
  367.         (play-sound 'balloon-help-appears))
  368.     (setq balloon-help-display-pending t))))
  369.  
  370. (defun balloon-help-set-frame-properties ()
  371.   (let ((frame balloon-help-frame))
  372.     ;; don't set the font unconditionally because it makes the
  373.     ;; frame size flap visibly while XEmacs figures out the new
  374.     ;; frame size.
  375.     (if (not (equal (face-font 'default frame) balloon-help-font))
  376.     (set-face-font 'default balloon-help-font frame))
  377.     (set-face-foreground 'default balloon-help-foreground frame)
  378.     (set-face-background 'default balloon-help-background frame)
  379.     (set-face-background 'modeline balloon-help-background frame)
  380.     (set-face-background-pixmap 'default balloon-help-background-pixmap frame)
  381.     (set-frame-property frame 'border-color balloon-help-border-color)
  382.     (set-frame-property frame 'border-width balloon-help-border-width)))
  383.  
  384. ;;;(defun balloon-help-resize-help-frame ()
  385. ;;;  (save-excursion
  386. ;;;    (set-buffer balloon-help-buffer)
  387. ;;;    (let ((longest 0)
  388. ;;;      (lines 0)
  389. ;;;      (done nil)
  390. ;;;      (window-min-height 1)
  391. ;;;      (window-min-width 1))
  392. ;;;      (goto-char (point-min))
  393. ;;;      (while (not done)
  394. ;;;    (end-of-line)
  395. ;;;    (setq longest (max longest (current-column))
  396. ;;;          done (not (= 0 (forward-line))))
  397. ;;;    (and (not done) (setq lines (1+ lines))))
  398. ;;;      (set-frame-size balloon-help-frame (+ 1 longest) lines))))
  399.  
  400. (defun balloon-help-resize-help-frame ()
  401.   (save-excursion
  402.     (set-buffer balloon-help-buffer)
  403.     (let* ((longest 0)
  404.        (lines 0)
  405.        (done nil)
  406.        (inst (vector 'string ':data nil))
  407.        (window (frame-selected-window balloon-help-frame))
  408.        (font-width (font-width (face-font 'default) balloon-help-frame))
  409.        start width
  410.        (window-min-height 1)
  411.        (window-min-width 1))
  412.       (goto-char (point-min))
  413.       (while (not done)
  414.     (setq start (point))
  415.     (end-of-line)
  416.     (aset inst 2 (buffer-substring start (point)))
  417.     (setq longest (max longest (glyph-width (make-glyph inst) window))
  418.           done (not (= 0 (forward-line))))
  419.     (and (not done) (setq lines (1+ lines))))
  420.       (setq width (/ longest font-width)
  421.         width (if (> longest (* width font-width)) (1+ width) width))
  422.       (set-frame-size balloon-help-frame (+ 0 width) lines))))
  423.  
  424. (defun balloon-help-compute-help-frame-y-location ()
  425.   (let* ((device-bottom (device-pixel-height
  426.              (frame-device balloon-help-frame)))
  427.      (y-pos (max 0 (+ 48 balloon-help-help-object-y)))
  428.      (height (frame-pixel-height balloon-help-frame))
  429.      (bottom (+ y-pos height)))
  430.     (if (>= bottom device-bottom)
  431.     (setq y-pos (max 0 (- y-pos (- bottom device-bottom)))))
  432.     y-pos ))
  433.  
  434. (defun balloon-help-compute-help-frame-x-location ()
  435.   (let* ((device-right (device-pixel-width (frame-device balloon-help-frame)))
  436.      (x-pos (max 0 (+ 32 balloon-help-help-object-x)))
  437.      (width (frame-pixel-width balloon-help-frame))
  438.      (right (+ x-pos width)))
  439.     (if (>= right device-right)
  440.     (setq x-pos (max 0 (- x-pos (- right device-right)))))
  441.     x-pos ))
  442.  
  443. (defun balloon-help-move-help-frame ()
  444.   (let ((x (balloon-help-compute-help-frame-x-location))
  445.     (y (balloon-help-compute-help-frame-y-location)))
  446.     (set-frame-position balloon-help-frame x y)))
  447.  
  448. (defun balloon-help-make-junk-frame ()
  449.   (let ((window-min-height 1)
  450.     (window-min-width 1))
  451.     (when (framep balloon-help-junk-frame)
  452.       (delete-frame balloon-help-junk-frame)
  453.       (setq balloon-help-junk-frame nil))
  454.     (prog1
  455.     (setq balloon-help-junk-frame
  456.           (make-frame '(minibuffer t
  457.                 initially-unmapped t
  458.                 width 1
  459.                 height 1)))
  460.       (set-window-buffer (frame-selected-window balloon-help-junk-frame)
  461.              balloon-help-buffer))))
  462.  
  463. (defun balloon-help-make-help-frame ()
  464.   (when (framep balloon-help-frame)
  465.     (delete-frame balloon-help-frame)
  466.     (setq balloon-help-frame nil))
  467.   (save-excursion
  468.     (set-buffer balloon-help-buffer)
  469.     (setq truncate-lines t)
  470.     (set-buffer-menubar nil)
  471.     (let* ((x (balloon-help-compute-help-frame-x-location))
  472.        (y (balloon-help-compute-help-frame-y-location))
  473.        (window-min-height 1)
  474.        (window-min-width 1)
  475.        (junk-frame (balloon-help-make-junk-frame))
  476.        (frame (make-frame (list
  477.                    '(initially-unmapped . t)
  478.                    ;; try to evade frame decorations
  479.                    (cons 'name balloon-help-frame-name)
  480.                    (cons 'border-width balloon-help-border-width)
  481.                    (cons 'border-color balloon-help-border-color)
  482.                    (cons 'top y)
  483.                    (cons 'left x)
  484.                    (cons 'popup junk-frame)
  485.                    (cons 'minibuffer
  486.                      (minibuffer-window junk-frame))
  487.                    '(width . 3)
  488.                    '(height . 1)
  489.                    '(balloon-help . t)))))
  490.       (set-face-font 'default balloon-help-font frame)
  491.       (set-face-foreground 'default balloon-help-foreground frame)
  492.       (set-face-background 'default balloon-help-background frame)
  493.       (set-face-background-pixmap 'default balloon-help-background-pixmap
  494.                   frame)
  495.       (set-window-buffer (frame-selected-window frame) balloon-help-buffer)
  496.       (set-specifier has-modeline-p (cons frame nil))
  497.       (when (featurep 'toolbar)
  498.     (set-specifier top-toolbar-height (cons frame 0))
  499.     (set-specifier left-toolbar-width (cons frame 0))
  500.     (set-specifier right-toolbar-width (cons frame 0))
  501.     (set-specifier bottom-toolbar-height (cons frame 0))
  502.     (set-specifier top-toolbar-visible-p (cons frame nil))
  503.     (set-specifier left-toolbar-visible-p (cons frame nil))
  504.     (set-specifier right-toolbar-visible-p (cons frame nil))
  505.     (set-specifier bottom-toolbar-visible-p (cons frame nil))
  506.     (set-specifier top-toolbar (cons frame nil))
  507.     (set-specifier left-toolbar (cons frame nil))
  508.     (set-specifier right-toolbar (cons frame nil))
  509.     (set-specifier bottom-toolbar (cons frame nil)))
  510.       (when (featurep 'scrollbar)
  511.     (set-specifier scrollbar-width (cons frame 0))
  512.     (set-specifier scrollbar-height (cons frame 0)))
  513.       (set-specifier text-cursor-visible-p (cons frame nil))
  514.       (set-specifier has-modeline-p (cons frame nil))
  515.       (set-specifier modeline-shadow-thickness (cons frame 0))
  516.       (set-specifier (glyph-image truncation-glyph) [nothing] frame '(x))
  517.       (set-face-background 'modeline balloon-help-background frame)
  518.       frame )))
  519.  
  520. (defun balloon-help-pre-command-hook ()
  521.   (unless (eq this-command 'balloon-help)
  522.     (balloon-help-go-away)))
  523.  
  524. (defun balloon-help-go-away (&rest ignored)
  525.   (setq balloon-help-help-object nil)
  526.   (if (balloon-help-displayed)
  527.       (balloon-help-undisplay-help)))
  528.  
  529. (defun balloon-help-mouse-leave-frame-hook (&rest ignored)
  530.   (let* ((mouse (mouse-position))
  531.      (window (car mouse)))
  532.     (if (or (null window) (not (eq (window-frame window) balloon-help-frame)))
  533.     (balloon-help-go-away))))
  534.  
  535. ;; loses with ClickToFocus under fvwm
  536. ;;(fset 'balloon-help-deselect-frame-hook 'balloon-help-go-away)
  537. ;;(add-hook 'deselect-frame-hook 'balloon-help-deselect-frame-hook)
  538.  
  539. (add-hook 'mouse-motion-hook 'balloon-help-motion-hook)
  540.  
  541. (add-hook 'pre-command-hook 'balloon-help-pre-command-hook)
  542. (add-hook 'mouse-leave-frame-hook 'balloon-help-mouse-leave-frame-hook)
  543.